package cn.kent.controller;

import cn.kent.context.MySessionContext;
import cn.kent.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/manager")
public class ManageController {

    /**
     * 模拟用户登录
     */
    @RequestMapping("/modifySession")
    public String modifySession(HttpSession session) {
        ServletContext servletContext = session.getServletContext();
        String userId = "1"; // 修改userId=1的用户
        String sessionId = (String) servletContext.getAttribute(userId);// 从servletContext上下文中根据userId获取sessionId
        System.out.println("sessionId:" + sessionId);

        HashMap<String, HttpSession> sessionMap = MySessionContext.sessionMap;// 获取sessionMap
        HttpSession currentSession = sessionMap.get(sessionId); // 根据sessionId获取用户session
        User user = (User) currentSession.getAttribute("currentUser");// 根据session获取用户信息
        user.setLevel("vip"); // 修改内容
        return "success";
    }

}
